package com.neuedu.mblog.dao;

import com.neuedu.mblog.entiry.UploadFile;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目：his
 * 创建时间：  2021/12/15   15:51
 * 作者 :jshand
 * 描述 :
 */
public class UploadFileDao  extends BaseDao<UploadFile> {


    @Override
    public List<UploadFile> selectList(String sql111) {
        Connection con = null;
        Statement stmt =null;
        ResultSet rs = null;
        List list = new ArrayList();
        try {
            con = getConnection();
            stmt = con.createStatement();

            String sql = " SELECT   id, origin_name,  path, upload_time, size, ip FROM   upload_files  ";
            rs = stmt.executeQuery(sql);
            UploadFile uploadFile = null;
            while (rs.next()){
                uploadFile = new UploadFile();
                uploadFile.setId(rs.getInt("id"));
                uploadFile.setOriginName(rs.getString("origin_name"));
                uploadFile.setPath(rs.getString("path"));
                uploadFile.setUploadTime(rs.getTimestamp("upload_time"));
                uploadFile.setSize(rs.getLong("size"));
                uploadFile.setIp(rs.getString("ip"));


                list.add(uploadFile);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        finally {
            close(con,stmt,rs);
        }

        return list;
    }

    @Override
    public List<UploadFile> selectList(String sql, Object... params) throws Exception {
        return null;
    }

    @Override
    public UploadFile selectById(Integer id) {
        Connection con = null;
        PreparedStatement stmt =null;
        ResultSet rs = null;
        UploadFile uploadFile = null;
        try {
            con = getConnection();

            String sql = " SELECT   id, origin_name,  path, upload_time, size, ip FROM   upload_files where id = ?  ";
            stmt = con.prepareStatement(sql);

            //绑定变量
            stmt.setInt(1,id);

            rs = stmt.executeQuery();

            if (rs.next()){
                uploadFile = new UploadFile();
                uploadFile.setId(rs.getInt("id"));
                uploadFile.setOriginName(rs.getString("origin_name"));
                uploadFile.setPath(rs.getString("path"));
                uploadFile.setUploadTime(rs.getTimestamp("upload_time"));
                uploadFile.setSize(rs.getLong("size"));
                uploadFile.setIp(rs.getString("ip"));

            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        finally {
            close(con,stmt,rs);
        }

        return uploadFile;
    }

    /**
     * 返回主键
     * @param uploadFile
     * @return
     */
    @Override
    public int save(UploadFile uploadFile) {
        Connection con = null;
        PreparedStatement stmt =null;
        ResultSet rs = null;
        int pk = -1;
        try {
            con = getConnection();

            //执行sql
            String sql = " INSERT INTO upload_files ( origin_name,path,size,ip)  VALUES  ( ?,?,?,? )  " ;
            stmt = con.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);

            stmt.setString(1,uploadFile.getOriginName());
            stmt.setString(2,uploadFile.getPath());
            stmt.setLong(3,uploadFile.getSize());
            stmt.setString(4,uploadFile.getIp());

            stmt.executeUpdate();

            ResultSet pkrs = stmt.getGeneratedKeys();
            pkrs.next();
            pk =  pkrs.getInt(1);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        finally {
            close(con,stmt,rs);
        }

        return pk;
    }

    @Override
    public int update(UploadFile uploadFile) {
        return -1;
    }

    @Override
    public int delete(Integer id) {
        return -1;
    }


}
